package com.lujianfei.powerinput.ui.template;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.lujianfei.powerinput.R;
import com.lujianfei.powerinput.base.BaseActivity;
import com.lujianfei.powerinput.ui.template.base.TemplateContract;
import com.lujianfei.powerinput.ui.template.presenter.TemplatePresenter;
import com.lujianfei.powerinput.widget.EmptyView;

import java.util.ArrayList;
import java.util.List;

public class TemplateActivity extends BaseActivity implements TemplateContract.View {

    public final static String OPER_TYPE = "OPER_TYPE";
    public final static String TYPE_SELECT = "TYPE_SELECT";
    private TemplateContract.Presenter presenter;
    private ListView listview;
    private ArrayAdapter adapter;
    private final List<String> data=new ArrayList<>();
    private String operType = "";
    private EmptyView emptyView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template);
        parseIntent();
        initView();
        initEvent();
        setTitleMiddleText("模板");
        showTitleRightImage();
        setTitleRightImage(R.drawable.ic_add);
        presenter = new TemplatePresenter(this);
    }

    private void parseIntent() {
        Intent intent = getIntent();
        operType = intent.getStringExtra(OPER_TYPE);
    }

    private void initEvent() {
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                presenter.onItemClick(adapterView,view,i,l);
            }
        });
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                presenter.onItemLongClick(adapterView,view,i,l);
                return true;
            }
        });
    }

    private void initView() {
        listview = findViewById(R.id.listview);
        emptyView = findViewById(R.id.emptyView);
    }

    @Override
    public void onTitleRightImageClick() {
        super.onTitleRightImageClick();
        presenter.onTitleRightImageClick();
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public Activity currentActivity() {
        return this;
    }


    @Override
    protected void onResume() {
        super.onResume();
        presenter.initData();
    }

    @Override
    public void updateList(List<String> strings) {
        if(null == adapter) {
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
            listview.setAdapter(adapter);
        }
        data.clear();
        data.addAll(strings);
        adapter.notifyDataSetChanged();
    }

    @Override
    public String getOperType() {
        return operType;
    }

    @Override
    public void showEmptyView() {
        View parent = (View) emptyView.getParent();
        parent.setVisibility(View.VISIBLE);
        emptyView.setTitleText("添加自己的模板");
        emptyView.setButtonText("走你");
        emptyView.showButton();
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onTitleRightImageClick();
            }
        });
    }

    @Override
    public void hideEmptyView() {
        View parent = (View) emptyView.getParent();
        parent.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }
}
